﻿using System.Reflection;
using RpcHelper;
using RpcHelper.Config;
using SqlSugar;

namespace SqlSugarHelper.Config
{
    internal class DbConfigFactory : IDBConfig
    {
        private static string[] _AllowSetPro = new string[]
        {
            "db:SqlSugar:IsAutoCloseConnection",
            "db:SqlSugar:InitKeyType",
             "db:SqlSugar:LanguageType",
             "db:SqlSugar:SlaveConnectionConfigs",
             "db:SqlSugar:IndexSuffix"
        };
        private List<ConnectionConfig> _Config;
        private DefDbConfig[] _DefConfig;
        private ISugarConfig _SugarConfig;
        public DbConfigFactory(IConfigCollect config, ISugarConfig sugarConfig)
        {
            this._SugarConfig = sugarConfig;
            this._DefConfig = config.GetValue<DefDbConfig[]>("db:SqlSugar", null);
            this._Init();
            config.RefreshEvent += _Refresh;
        }
        private void _Init()
        {
            if (_DefConfig.IsNull())
            {
                _Config = null;
                return;
            }
            this._Config = _DefConfig.Select(c =>
            {
                ConnectionConfig config = new ConnectionConfig
                {
                    IndexSuffix = c.IndexSuffix,
                    ConnectionString = c.ConnectionString,
                    ConfigId = c.ConfigId ?? "default",
                    IsAutoCloseConnection = c.IsAutoCloseConnection,
                    DbType = c.DbType,
                    LanguageType = c.LanguageType
                };
                ConMoreSet moreSet = c.MoreSettings;
                if (moreSet == null)
                {
                    moreSet = new ConMoreSet();
                }
                config.MoreSettings = new ConnMoreSettings
                {
                    DbMinDate = moreSet.DbMinDate,
                    DefaultCacheDurationInSeconds = moreSet.DefaultCacheDurationInSeconds,
                    PgSqlIsAutoToLower = moreSet.PgSqlIsAutoToLower,
                    TableEnumIsString = moreSet.TableEnumIsString,
                    DisableMillisecond = moreSet.DisableMillisecond,
                    DisableNvarchar = moreSet.DisableNvarchar,
                    IsAutoRemoveDataCache = moreSet.IsAutoRemoveDataCache,
                    IsWithNoLockQuery = moreSet.IsWithNoLockQuery
                };
                if (!c.SlaveConnectionConfigs.IsNull())
                {
                    config.SlaveConnectionConfigs = new List<SlaveConnectionConfig>(c.SlaveConnectionConfigs.Length);
                    c.SlaveConnectionConfigs.ForEach(d =>
                    {
                        config.SlaveConnectionConfigs.Add(new SlaveConnectionConfig
                        {
                            ConnectionString = d.ConnectionString,
                            HitRate = d.HitRate
                        });
                    });
                }
                config.AopEvents = new AopService(this._SugarConfig);
                return config;
            }).ToList();
        }
        private void _EntityFilter(PropertyInfo pro, EntityColumnInfo entity)
        {
            //if(!entity.IsJson && Type.GetTypeCode(pro.PropertyType) == TypeCode.Object && pro.PropertyType != PublicDataDic.GuidType)
            //{
            //    entity.IsJson = true;
            //}
        }
        private void _Refresh(IConfigCollect config, string name)
        {
            if (_AllowSetPro.Contains(name))
            {
                this._DefConfig = config.GetValue<DefDbConfig[]>("db:SqlSugar");
                this._Init();
            }
        }
        public List<ConnectionConfig> Configs
        {
            get => this._Config;
        }
    }
}
